/*
	FileName : JT_C_TAX.c
	Description :
		Manage the JT_C TAX opperations.
	
	Created on 2017.12.02 by WFL.

	Revision History:
		

*/


/* Includes ------------------------------------------------------------------*/
#include "JT_C_TAX.h"

//---------------Global--------------------------------
uint8_t JTC_TAX_DATA[JTC_TAX_BUF_MAX_LEN];

//---------------Local--------------------------------
uint8_t	JTC_TAX_receiveBuf[JTC_TAX_BUF_MAX_LEN];
uint8_t JTC_TAX_receiveLen = 0;
uint8_t JTC_TAX_checkSum = 0;	//CKS
//----------------------------------------------------
#define JTC_38_OFFSET_chezhong	6
//-------------------------------
#define JTC_39_OFFSET_TIME		(TAX38_FRAME_LEN + 3)
#define JTC_39_OFFSET_SPEED		(TAX38_FRAME_LEN + 7)
#define JTC_39_OFFSET_KMPOST	(TAX38_FRAME_LEN + 15)
#define JTC_39_OFFSET_JCH		(TAX38_FRAME_LEN + 32)
#define JTC_39_OFFSET_JCTYPE	(TAX38_FRAME_LEN + 34)
#define JTC_39_OFFSET_DEVICE	(TAX38_FRAME_LEN + 37)
#define JTC_39_OFFSET_DEVICE_b_jiangji	0x01	//1: jiangji ; 0: jiankong
#define JTC_39_OFFSET_DEVICE_b_diaoche	0x04	//1: diaoche ; 0: fei diaoche

//-------------------------------
#define JTC_F0_OFFSET_TIME		4
#define JTC_F0_OFFSET_SPEED		8
#define JTC_F0_OFFSET_KMPOST	17
#define JTC_F0_OFFSET_DEVICE	30
#define JTC_F0_OFFSET_DEVICE_b_jiankong	0x04	//1: jiankong ; 0: jiangji
#define JTC_F0_OFFSET_DEVICE_b_Ndiaoche	0x02	//1: fei diaoche ; 0: diaoche
#define JTC_F0_OFFSET_chezhong	48
#define JTC_F0_OFFSET_JCTYPE	62
#define JTC_F0_OFFSET_JCH		64
//-------------------------------


//----------------------------------------------------
#define JTC_TAX_TYPE_NUMBER	5

#define JTC_TAX_TYPE_NO		0
#define JTC_TAX_TYPE_38		1
#define JTC_TAX_TYPE_39		2
#define JTC_TAX_TYPE_F0		3
#define JTC_TAX_TYPE_FA		4

const uint8_t JTC_TAX_TYPE_MAP[JTC_TAX_TYPE_NUMBER] = {0,0X38,0X39,0XF0,0XFA};
const uint8_t JTC_TAXFrameLen[JTC_TAX_TYPE_NUMBER] = {	0,
														TAX38_FRAME_LEN, 
														TAX39_FRAME_LEN, 
														TAX_FRAME_LEN, 
														TAX_FRAME_LEN};

uint8_t TAX_tyep = JTC_TAX_TYPE_NO;

//---------------------------------------

uint8_t JTC_TAX_cacheData(uint16_t data);
void JTC_TAX_CopyTaxBuf(void);
//----------------------------------------------------------------
//LittleEndian
uint32_t JTC_TAX_LE_Get32(uint8_t *itemStartAddr, uint16_t offset)
{
	uint8_t *addr;
	uint32_t returnValue = 0;
	
	addr = itemStartAddr + offset;
	
	returnValue += addr[3];
	returnValue <<= 8;
	returnValue += addr[2];
	returnValue <<= 8;
	returnValue += addr[1];
	returnValue <<= 8;
	returnValue += addr[0];
	
	return 	returnValue;
}
//-----------------------------------------------
void JTC_TAX_clear_TAX_DATA(void)
{
	uint8_t  i;
	for(i = 0; i < JTC_TAX_BUF_MAX_LEN; i++)
	{
		JTC_TAX_DATA[i] = 0;
	}
}
uint8_t JTC_TAX_getTAXtype(uint16_t head)
{
	uint8_t type;
	for(type = JTC_TAX_TYPE_NO; type < JTC_TAX_TYPE_NUMBER; type++)
	{
		if(JTC_TAX_TYPE_MAP[type] == (head & 0xFF))
			return type;
	}
	return JTC_TAX_TYPE_NO;
}
uint8_t JTC_TAX_ReceiveData(uint16_t data)
{
	uint8_t re = 0,type;
	switch (data){
			
		case 0x138:
		case 0x139:
		case 0x1F0:
		case 0x1FA:

			TAX_tyep = JTC_TAX_getTAXtype(data);
			JTC_TAX_receiveBuf[0] = data & 0xff;
			JTC_TAX_receiveLen = 1;
			JTC_TAX_checkSum = data & 0xff;
			break;
		
		default:
			type = JTC_TAX_cacheData(data);
			if(type == JTC_TAX_TYPE_39 || \
				type == JTC_TAX_TYPE_F0 || \
				type == JTC_TAX_TYPE_FA)
				re = 1;
			break;
	}
	return re;
}
//---------------------------------------------

uint8_t JTC_TAX_cacheData(uint16_t data)
{
	uint8_t re = 0;
	if (TAX_tyep != JTC_TAX_TYPE_NO){
			
		JTC_TAX_receiveBuf[JTC_TAX_receiveLen++] = data & 0xff;
		JTC_TAX_checkSum += (data & 0xff);
		
		if (JTC_TAX_receiveLen >= JTC_TAXFrameLen[TAX_tyep]){
				
			if (!JTC_TAX_checkSum)
			{
				JTC_TAX_CopyTaxBuf();
				re = TAX_tyep;
			}
			TAX_tyep = 0;
			JTC_TAX_receiveLen = 0;
			JTC_TAX_checkSum = 0;
		}
	}
	return re;
}

void JTC_TAX_CopyTaxBuf(void)
{
	uint8_t i;
	switch (TAX_tyep){
		
		case JTC_TAX_TYPE_38:///38
			
			for (i = 0; i < TAX38_FRAME_LEN; i++)
				JTC_TAX_DATA[i] = JTC_TAX_receiveBuf[i];
			break;
		
		case JTC_TAX_TYPE_39:///39
			
			for (i = 0; i < TAX39_FRAME_LEN; i++)
				JTC_TAX_DATA[TAX38_FRAME_LEN + i] = JTC_TAX_receiveBuf[i];
			break;
		
		case JTC_TAX_TYPE_F0:///F0
		case JTC_TAX_TYPE_FA:///FA
			for (i = 0; i < TAX_FRAME_LEN; i++)
				JTC_TAX_DATA[i] = JTC_TAX_receiveBuf[i];
			break;
					
		default:
			break;
	}				
}

//-----------------------------------------------------------------------
void JTC_TAX_getKmPost(uint8_t* data)
{
	uint8_t type;
	
	data[0] = 0;
	data[1] = 0;
	data[2] = 0;
	
	type = JTC_TAX_getTAXtype(JTC_TAX_DATA[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(JTC_TAX_DATA[TAX38_FRAME_LEN + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			data[0] = JTC_TAX_DATA[JTC_39_OFFSET_KMPOST + 0];
			data[1] = JTC_TAX_DATA[JTC_39_OFFSET_KMPOST + 1];
			data[2] = JTC_TAX_DATA[JTC_39_OFFSET_KMPOST + 2];
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		data[0] =  (JTC_TAX_DATA[JTC_F0_OFFSET_KMPOST + 0]);
		data[1] =  (JTC_TAX_DATA[JTC_F0_OFFSET_KMPOST + 1]);
		data[2] =  (JTC_TAX_DATA[JTC_F0_OFFSET_KMPOST + 2] & 0x7f);
		data[2] |= (JTC_TAX_DATA[JTC_F0_OFFSET_KMPOST + 3] & 0x80);
	}
}

uint8_t JTC_TAX_checiValid(void)
{
	uint8_t type,i,tmp;
	uint8_t valid = 0;
	
	type = JTC_TAX_getTAXtype(JTC_TAX_DATA[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(JTC_TAX_DATA[TAX38_FRAME_LEN + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			for(i = 0; i < 4; i++)
			{
				tmp = JTC_TAX_DATA[JTC_38_OFFSET_chezhong + i];
				if(tmp == 0x20 || (tmp >= 'A' && tmp <= 'Z'))
				{
					valid++;
				}
			}
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		for(i = 0; i < 4; i++)
		{
			tmp = JTC_TAX_DATA[JTC_F0_OFFSET_chezhong + i];
			if(tmp == 0x20 || (tmp >= 'A' && tmp <= 'Z'))
			{
				valid++;
			}
		}
	}
	if(valid == 4)
		return 1;
	return 0;
}
uint32_t JTC_TAX_getTime(void)
{
	uint8_t type;
	uint32_t time = 0;
	
	type = JTC_TAX_getTAXtype(JTC_TAX_DATA[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(JTC_TAX_DATA[TAX38_FRAME_LEN + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			time = JTC_TAX_LE_Get32(JTC_TAX_DATA,JTC_39_OFFSET_TIME);
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		time = JTC_TAX_LE_Get32(JTC_TAX_DATA,JTC_F0_OFFSET_TIME);
	}
	return time;
}
//---------------------------------------------------------------------





///EOF
